package com.boolib.animate.framework.web;

import com.alibaba.fastjson.JSON;
import com.boolib.animate.commons.exception.UserAuthenticationException;
import com.boolib.animate.framework.entity.AnimateCollectWork;
import com.boolib.animate.framework.entity.AnimateUser;
import com.boolib.animate.framework.entity.AnimateWork;
import com.boolib.animate.framework.mapper.AnimateCollectWorkMapper;
import com.boolib.animate.framework.service.advice.AnimateCollectWorkService;
import com.boolib.animate.framework.service.base.impl.BaseAnimateCollectWorkService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author nier
 * @create 2020-03-02 23:54
 */
@RequestMapping("/api/collect")
@RestController
public class AnimateCollectWorkController {

    @Autowired
    private HttpSession httpSession;
    @Autowired
    private AnimateCollectWorkService animateCollectWorkService;

    @GetMapping
    public List<AnimateWork> queryUserCollect(){
        final AnimateUser current = (AnimateUser)httpSession.getAttribute("current");
        if(current==null){
            throw new UserAuthenticationException();
        }
        return  animateCollectWorkService.queryCollect(current.getId());
    }

    @GetMapping("{workId}")
    public boolean queryUserIsCollect(@PathVariable("workId")Long workId){
        final AnimateUser current = (AnimateUser)httpSession.getAttribute("current");
        System.out.println(JSON.toJSONString(current));
        if(current==null){
            throw new UserAuthenticationException();
        }
        return  animateCollectWorkService.isCollect(current.getId(),workId);
    }
    @PostMapping("{workId}")
    public boolean newCollect(@PathVariable("workId")Long workId){
        final AnimateUser current = (AnimateUser)httpSession.getAttribute("current");
        System.out.println(JSON.toJSONString(current));
        if(current==null){
            throw new UserAuthenticationException();
        }
        return  animateCollectWorkService.collect(current.getId(),workId);
    }
}
